#ifndef __M_UTIL_H__
#define __M_UTIL_H__

#include <mysql/mysql.h>
#include <jsoncpp/json/json.h>
#include <string>
#include <vector>
#include <memory>
#include <sstream>
#include <iostream>
#include <fstream>
#include <websocketpp/server.hpp>
#include <websocketpp/config/asio_no_tls.hpp>

#include "log.hpp"
typedef websocketpp::server<websocketpp::config::asio> websocket_server;

// mysql工具类
class mysql_util
{
public:
    // 链接数据库创建句柄
    static MYSQL *mysql_creat(const std::string &host,
                              const std::string &username,
                              const std::string &password,
                              const std::string &dbname,
                              uint16_t port = 3306)
    {
        MYSQL *mysql = mysql_init(nullptr);
        if (mysql == nullptr)
        {
            ELOG("mysql init failed!");
            return nullptr;
        }
        // 链接服务器
        if (mysql_real_connect(mysql, host.c_str(), username.c_str(), password.c_str(), dbname.c_str(), port, nullptr, 0) == nullptr)
        {
            ELOG("connect mysql servwe failed : %s", mysql_error(mysql));
            mysql_close(mysql);
            return nullptr;
        }
        // 设置客户端字符集
        if (mysql_set_character_set(mysql, "utf8") != 0)
        {
            ELOG("set client character failed : %s", mysql_error(mysql));
            mysql_close(mysql);
            return nullptr;
        }
        return mysql;
    }

    // 操作数据库
    static bool mysql_exec(MYSQL *mysql, const std::string &sql)
    {
        int ret = mysql_query(mysql, sql.c_str());
        if (ret != 0)
        {
            ELOG("%s\n", sql.c_str());                             // 错误语句
            ELOG("mysql query failed : %s\n", mysql_error(mysql)); // 错误消息
            return false;
        }
        return true;
    }

    // 销毁
    static void mysql_destroy(MYSQL *mysql)
    {
        if (mysql != nullptr)
        {
            mysql_close(mysql);
        }
        return;
    }
};

// josn工具
class json_util
{

public:
    // 序列化
    static bool serialize(const Json::Value &root, std::string &str)
    {
        Json::StreamWriterBuilder swb;
        std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
        std::stringstream ss;

        if (sw->write(root, &ss) != 0)
        {
            ELOG("json serialize failed!! \n string:%s,", str);
            return false;
        }
        str = ss.str();
        return true;
    }

    // 反序列化
    static bool unserialize(const std::string &str, Json::Value &root)
    {
        Json::CharReaderBuilder crb;
        std::unique_ptr<Json::CharReader> cr(crb.newCharReader());
        std::string err;
        if (cr->parse(str.c_str(), str.c_str() + str.size(), &root, &err) == false)
        {
            ELOG("json unserialize failed!! err:%s\n string:%s,", err, str);
            return false;
        }
        return true;
    }
};

// string工具
class string_util
{
public:
    // 将src字符串，以sep为分隔符分割，装入res中
    static int split(const std::string &in, const std::string &sep,
                     std::vector<std::string> &arry)
    {
        arry.clear();
        size_t pos, idx = 0;
        while (idx < in.size())
        {
            pos = in.find(sep, idx);
            if (pos == std::string::npos)
            {
                arry.push_back(in.substr(idx));
                break;
            }

            if (pos != idx)
            {
                arry.push_back(in.substr(idx, pos - idx));
            }
            idx = pos + sep.size();
        }
        return arry.size();
    }

};

// file工具
class file_util
{
public:
    static bool read(const std::string &filename, std::string &body)
    {
        // 打开文件(使用二进制打开，防止数据在读时被自动修改 -》 对原数据操作)
        std::ifstream ifs(filename, std::ios::binary);
        if (ifs.is_open() == false)
        {
            ELOG("%s file open failed!!", filename.c_str());
            return false;
        }
        // 获取文件大小
        size_t fsize = 0;
        ifs.seekg(0, std::ios::end); // 跳转文件末尾
        fsize = ifs.tellg();
        ifs.seekg(0, std::ios::beg);

        body.resize(fsize);
        // 读取
        ifs.read(&body[0], fsize); // body.c_str()为const
        if (ifs.good() == false)
        {
            ELOG("read file %s content failed!", filename.c_str());
            ifs.close();
            return false;
        }
        // 关闭
        ifs.close();
        return true;
    }
};
#endif